
package combat.dudes.client.net.threads;

import combat.dudes.DudesFunctions;
import combat.dudes.client.ClientApp;
import combat.dudes.protocol.data.ChatEntry;


/**
 * The NET_CHAT thread creates an instance of this class, providing chat-functionality and -communications with the server
 * @author Laurens Andriessen
 * @author Lex Tas
 */
public class NetChat {

	public NetChat() {
		// Chat (single, static instance)
		int lastKnownMessageID = -1;

		while(true) {
			long start = DudesFunctions.DateTime.getTimeInMs();

			if(ClientApp.SharedVars.chat != null) { // Check chat-cajo-binding instance

				// Loop through all new chats and add them
				for(ChatEntry chat : ClientApp.SharedVars.chat.getNewChats(lastKnownMessageID)) {
					ClientApp.SharedVars.addChat(chat);
					ClientApp.Log("Added chat message (<" + chat.getUserName() + ">: '" + chat.getMessage() + "')");
				}

				// If the chat-variable is not empty, put the last-known-message-id to the last message or else retrieve all chats again
				if(ClientApp.SharedVars.getChats().size() > 0)
					lastKnownMessageID = ClientApp.SharedVars.getChats().get(ClientApp.SharedVars.getChats().size() - 1).getMessageID();
				else lastKnownMessageID = -1;
			}

			DudesFunctions.Status.DelayUpdate(start, 1000, Thread.currentThread());
		}
	}
}
